//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UniformGridMesher.h"
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpIterator.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpLayout.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (boost::shared_ptr<QuantLib::UniformGridMesher>& childNative, Object^ owner) : CFdmMesher(CUniformGridMesher::typeid)
{
#ifdef HANDLE
	_phUniformGridMesher = NULL;
#endif
	_ppUniformGridMesher = &childNative;
    _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
}
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (QuantLib::UniformGridMesher& childNative, Object^ owner) : CFdmMesher(CUniformGridMesher::typeid)
{
#ifdef HANDLE
	_phUniformGridMesher = NULL;
#endif
	_ppUniformGridMesher = new boost::shared_ptr<QuantLib::UniformGridMesher> (&childNative);
    _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
    _UniformGridMesherOwner = owner;
    _FdmMesherOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (CUniformGridMesher^ copy) : CFdmMesher(CUniformGridMesher::typeid)
{
#ifdef HANDLE
	_phUniformGridMesher = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUniformGridMesher = new boost::shared_ptr<QuantLib::UniformGridMesher> (copy->GetShared());
        _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
    }
}
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (PLATFORM::Type^ t) : CFdmMesher(CUniformGridMesher::typeid)
{
#ifdef HANDLE
	_phUniformGridMesher = NULL;
#endif
	if (!t->IsSubclassOf(CUniformGridMesher::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (QuantLib::Handle<QuantLib::UniformGridMesher>& childNative, Object^ owner)  : CFdmMesher(CUniformGridMesher::typeid)
{
	_phUniformGridMesher = &childNative;
	_ppUniformGridMesher = &static_cast<boost::shared_ptr<QuantLib::UniformGridMesher>>(childNative.currentLink());
    _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
    _UniformGridMesherOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (QuantLib::Handle<QuantLib::UniformGridMesher> childNative)  : CFdmMesher(CUniformGridMesher::typeid)
{
	_phUniformGridMesher = &childNative;
	_ppUniformGridMesher = &static_cast<boost::shared_ptr<QuantLib::UniformGridMesher>>(childNative.currentLink());
    _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::CUniformGridMesher (QuantLib::UniformGridMesher childNative)  : CFdmMesher(CUniformGridMesher::typeid)
{
#ifdef HANDLE
	_phUniformGridMesher = NULL;
#endif
	_ppUniformGridMesher = new boost::shared_ptr<QuantLib::UniformGridMesher> (new QuantLib::UniformGridMesher (childNative));
    _ppFdmMesher = new boost::shared_ptr<QuantLib::FdmMesher> (boost::dynamic_pointer_cast<QuantLib::FdmMesher> (*_ppUniformGridMesher));
}
#endif

Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::~CUniformGridMesher ()
{
    if (_ppUniformGridMesher != NULL)
    {
	    delete _ppUniformGridMesher;
        _ppUniformGridMesher = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::!CUniformGridMesher ()
{
    if (_ppUniformGridMesher != NULL)
    {
	    delete _ppUniformGridMesher;
    }
}
QuantLib::UniformGridMesher& Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::GetReference ()
{
    if (_ppUniformGridMesher == NULL) throw REFNEW NativeNullException ();
	return **_ppUniformGridMesher;
}
boost::shared_ptr<QuantLib::UniformGridMesher>& Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::GetShared ()
{
    if (_ppUniformGridMesher == NULL) throw REFNEW NativeNullException ();
	return *_ppUniformGridMesher;
}
QuantLib::UniformGridMesher* Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::GetPointer ()
{
    if (_ppUniformGridMesher == NULL) throw REFNEW NativeNullException ();
	return &**_ppUniformGridMesher;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UniformGridMesher>& Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::GetHandle ()
{
	if (_phUniformGridMesher == NULL)
	{
		_phUniformGridMesher = new Handle<QuantLib::UniformGridMesher> (*_ppUniformGridMesher);
	}
	return *_phUniformGridMesher;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::HasNative () 
{
	return (_ppUniformGridMesher != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::Dminus (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ prm1, UInt64 direction)
{
    CFdmLinearOpIterator^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CFdmLinearOpIterator^> (prm1);
        _Cprm1->Lock();
        QuantLib::FdmLinearOpIterator& _prm1 = static_cast<QuantLib::FdmLinearOpIterator&> (_Cprm1->GetReference ()); 
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppUniformGridMesher)->dminus ( _prm1,  _direction );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Double Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::Dplus (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ prm1, UInt64 direction)
{
    CFdmLinearOpIterator^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CFdmLinearOpIterator^> (prm1);
        _Cprm1->Lock();
        QuantLib::FdmLinearOpIterator& _prm1 = static_cast<QuantLib::FdmLinearOpIterator&> (_Cprm1->GetReference ()); 
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppUniformGridMesher)->dplus ( _prm1,  _direction );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Double Cephei::QL::Methods::Finitedifferences::Meshers::CUniformGridMesher::Location (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, UInt64 direction)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppUniformGridMesher)->location ( _iter,  _direction );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

